/*************************************************************************/
/*									 */
/*	Routines to manage tree growth, pruning and evaluation		 */
/*	------------------------------------------------------		 */
/*									 */
/*************************************************************************/


#include "defns.i"
#include "types.i"
#include "extern.i"


ItemNo		*TargetClassFreq;


/* added by kun zhang*/
float           **LeafClassDist;




/*************************************************************************/
/*                      Evaluation routine of RDT                        */
/*      For each case generate the average class posterior probability   */
/*      taken into account of the class posterior prob produced by each  */
/*      RDTs. Coded by Kun Zhang                                         */
/*************************************************************************/
            
             
RDT_Evaluate(CMInfo)
    Boolean CMInfo;
{
    ClassNo RealClass, RDT_Category();
    short t;
    ItemNo *Confusion,k, i,p, RawErrors;
    
    float **TotalSum;
    Boolean PrintFlag;  
    ClassNo c,Att,ClassLabel;
    char Fn[100];
    FILE *OutFile, *fopen();
    

    Confusion =(ItemNo*)calloc((MaxClass+1)*(MaxClass+1),sizeof(ItemNo));
    
    
    if(CMInfo){     
  
    strcpy(Fn, FileName);
    if(FullTree)
    strcat(Fn, ".rdf");
    else
       strcat(Fn, ".rdh");
    if ( ! ( OutFile = fopen(Fn, "w") ) ) Error(0, Fn, "");
    
    ForEach(k,0,MaxClass)
         fprintf(OutFile, "%s ", ClassName[k]);
    fprintf(OutFile,"\n");
    
    
    }

                
    TotalSum =(float**)calloc(MaxItem+1,sizeof(float));
    ForEach(i,0,MaxItem){
        TotalSum[i] = (float *) calloc((MaxClass+1), sizeof(float));
        ForEach(c,0,MaxClass)
           TotalSum[i][c]=0.0;
    }

    
    LeafClassDist=(float **)calloc(MaxItem+1,sizeof(float*));
    ForEach(k,0,MaxItem)
	LeafClassDist[k]=(float*)calloc(MaxClass+1,sizeof(float));
        
	                        
                 
    /* just added for RDT display**************************/

    RawErrors = 0;
    
    ForEach(i,0,MaxItem)
        {  
    	
            ForEach(t, 0, TRIALS-1)
               {
                  RDT_Category(Item[i], Raw[t],LeafClassDist[i]);
                  ForEach(c,0,MaxClass)
                       TotalSum[i][c]+=LeafClassDist[i][c];
               }
            RealClass = Class(Item[i]);
                             
            PrintFlag = true;
            ForEach(c,0,MaxClass)
                {
                    if(PrintFlag)
                        {
                            Verbosity(2)              
                            printf("\n %d\t  %s\t\t %f", i,ClassName[c],TotalSum[i][c]/(float)TRIALS);
                            PrintFlag = false;

			    if(CMInfo)
				{
				    fprintf(OutFile,"%f ",TotalSum[i][c]/(float)TRIALS );
				}
                        }
                    else{
                          Verbosity(2)
			      printf("\n \t  %s\t\t %f", ClassName[c],TotalSum[i][c]/(float)TRIALS);
			  if(CMInfo)
			    fprintf(OutFile,"%f ", TotalSum[i][c]/(float)TRIALS );
                        }
                        
            }
            if(CMInfo) 
		fprintf(OutFile,"\n");
    


            ClassLabel=0;
            ForEach(k,1,MaxClass)
                {
                    if(TotalSum[i][k] > TotalSum[i][ClassLabel])
                        ClassLabel = k;
		    Verbosity(2)
			printf("\n\t\t\t\t\t\t>> %s\n", ClassName[ClassLabel]);
		    /* if(CMInfo)
			fprintf(OutLabel, "%s\n", ClassName[ClassLabel]);
		    */
		       
                    
                }
            if ( ClassLabel != RealClass ) RawErrors++;
	
                    
	    
         Confusion[RealClass*(MaxClass+1)+ClassLabel]++;                   
        }

    if(CMInfo){
	fflush(OutFile);
	fclose(OutFile);

    }
 
    
    printf("\nConfusion Matrix:\n");
    PrintConfusionMatrix(Confusion);
    

    printf("\n---------------------------------------------\n");
    printf("\tNoTrees         Errors(ErrorRate)\n");        
   
    	
    printf("\t%4d          %3d(%4.3f%%)\n", TRIALS,RawErrors, 100.0*RawErrors / (MaxItem+1.0));
 
    printf("\n---------------------------------------------\n");
    

  free(LeafClassDist);
  
  
  
  free(TotalSum);
 
}


/*************************************************************************/
/*		Shuffle the data items randomly				 */
/*									 */
/*************************************************************************/


    Shuffle()
/*  -------  */
{
    ItemNo This, Alt, Left;
    Description Hold;

    This = 0;
    for( Left = MaxItem+1 ; Left ; )
    {
        Alt = This + (Left--) * Random;
        Hold = Item[This];
        Item[This++] = Item[Alt];
        Item[Alt] = Hold;
    }
}








